var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkword: "",
    searchtype:[
      { picimg: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/%E6%96%B0%E7%89%88%E5%8C%BB%E7%96%97icon/yisheng.png?sign=d13163b9e958d543e5065076e94ed61e&t=1591607233", names: "医生" },
      { picimg: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/%E6%96%B0%E7%89%88%E5%8C%BB%E7%96%97icon/yiyuan.png?sign=75d413bcae7e1092228ace45acc5d024&t=1591607253", names: "医院" },
      // { picimg:"https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/%E6%96%B0%E7%89%88%E5%8C%BB%E7%96%97icon/yiyaopin.png?sign=4b09909458316c253eafc286ff6b0202&t=1591607272",names:"药品"},
    ],
    searchArr: [],
    resultif: false,
    searchtab:['推荐','医生','医院'],
    searchindex:0,
    sindex:0,
    zixun:[{},{},{},{}],
    problem:[{},{},{},{}],
    scrollTop: 0,
    searchlists: [
      { names: "泰尔丝(一维A酸软胶囊)", citys: "上海宜信延安药业有限公司", grade: true },
      { names: "桂曼(盐酸米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "泰尔丝(一维A酸软胶囊)", citys: "上海宜信延安药业有限公司", grade: true },
      { names: "桂曼(盐酸米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "泰尔丝(一维A酸软胶囊)", citys: "上海宜信延安药业有限公司", grade: true },
      { names: "桂曼(盐酸米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "泰尔丝(一维A酸软胶囊)", citys: "上海宜信延安药业有限公司", grade: true },
      { names: "桂曼(盐酸米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
      { names: "永喜(盐酸多米诺胶囊)", citys: "浩辉制药有限公司", grade: false },
    ],
  },


  lookzixun(e){
    wx.navigateTo({
      url: '/hyb_yl/tabBar/jibingyufang/jibingyufang?index=1',
    })
  },
  lookask(){
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/publicProblems/publicProblems',
    })
  },
  doctoritem(e){
    var zid = e.currentTarget.dataset.zid;
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid='+zid,
    })
  },

  btdetail(e){
    var id = e.currentTarget.dataset.id;
    var p_id = e.currentTarget.dataset.p_id;
    var zid = e.currentTarget.dataset.zid;
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunanlixq/zixunanlixq?id=' + id + '&p_id=' + p_id+'&zid='+zid,
    })
  },

  // 搜索类型
  typesbtn(e){
    if(e.currentTarget.dataset.dex == "0"){
      wx.navigateTo({
        url: '../searchyisheng/searchyisheng',
      })
    } else if (e.currentTarget.dataset.dex == "1"){
      wx.navigateTo({
        url: '../searchyiyuan/searchyiyuan',
      })
    } else if (e.currentTarget.dataset.dex == "2") {
      wx.navigateTo({
        url: '../searchyaopin/searchyaopin',
      })
    }
  }, 
  getSearch: function (e) {
    var that = this;
    var keywords = e.detail.value;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.show_keywordssearch',
      'data':{
        keywords:keywords
      },
      success: function (res) {
        console.log(res.data)
        if(res.data.length>0){
          that.setData({
            checkword: keywords,
            resultif: false,
            searchlisttip:res.data
          })
        }else{
          that.setData({
            checkword: keywords,
            resultif: true,
          })
          app.util.request({
            'url': 'entry/wxapp/jiansuo.addsearch',
            'data':{
              openid: wx.getStorageSync('openid'),
              checkword:keywords,
              source:0,
            },
          })
          that.getSearchlist();
        }
        
      },
    })

  },
  listitembtn(e) {
    var that = this;
    var checkword = e.currentTarget.dataset.checkword;
    console.log(checkword);
    app.util.request({
      'url': 'entry/wxapp/jiansuo.addsearch',
      'data':{
        openid: wx.getStorageSync('openid'),
        checkword:checkword,
        source:0,
      },
    })
    that.setData({
      resultif: true,
      checkword:checkword,
    })
    that.getSearchlist()
  },
  del: function () {
    var searchArr = this.data.searchArr;
    this.setData({
      searchArr: [],
      searchCont:""
    })
    wx.setStorage({
      key: 'searchArr',
      data: this.data.searchArr,
    })
  },
  searchHistory:function(e){
    var that = this;
    var checkword = e.currentTarget.dataset.checkword;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.addsearch',
      'data':{
        openid: wx.getStorageSync('openid'),
        checkword:checkword,
        source:0,
      },
    })
    that.setData({
      resultif: true,
      checkword:checkword,
    })
    that.getSearchlist()
  },
  searchtabs:function(e){
    var that = this;
    const index = e.currentTarget.dataset.index;
    that.setData({
      searchindex:index,
      sindex:index,
    })
    that.getSearchlist()
  },
  // swipertab:function(e){
  //   var that = this;
  //   const index = e.detail.current;
  //   that.setData({
  //     searchindex: index,
  //     sindex:index
  //   })
  //   that.getSearchlist()
  // },
  scroll: function (e) {
    var self = this;
    setTimeout(function () {
      console.log(e.detail.scrollTop);
      var list = self.data.list;
      if (list[self.data.curListId]) {
        list[self.data.curListId].scrollTop = e.detail.scrollTop;
        self.setData({
          list
        })
      }
    }, 300);
  },
  listitembtns(e) {
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/specialtyContent/specialtyContent?hid='+hid,
    })
  },
  
  listitembtninfor(e) {
    wx.navigateTo({
      url: '../yaopindetail/yaopindetail?name=' + e.currentTarget.dataset.name,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var searchArr = wx.getStorageSync("searchArr") || []
    that.setData({
      searchArr:searchArr
    })
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })

    that.getHotsearch();
    that.getHistorysearch();
  },


  getHotsearch:function(){
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.show_hotsearch',
      'data':{
        source:0
      },
      success: function (res) {
        that.setData({
          findArr:res.data
        })
      },
    })
  },
  getHistorysearch:function(){
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.show_historysearch',
      'data':{
        openid:wx.getStorageSync('openid'),
        source:0
      },
      success: function (res) {
        that.setData({
          searchArr:res.data
        })
      },
    })
  },


  getSearchlist:function(){
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.searchlist',
      'data':{
        openid:wx.getStorageSync('openid'),
        checkword:that.data.checkword,
        source:0,
        sindex:that.data.sindex,
      },
      success: function (res) {
        if(that.data.sindex==0){
          that.setData({
            zixunlist:res.data.zixunlist,
            answerlist:res.data.answerlist
          })
        }
        if(that.data.sindex==1){
          that.setData({
            zhuanjialist:res.data.zhuanjialist,
          })
        }
        if(that.data.sindex==2){
          that.setData({
            hospitalist:res.data.yiyuanlist,
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})